package com.hn.springframework.webmvc.servlet;

import com.hn.springframework.annotation.HNRequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class HNHandlerAdapter {

    public boolean supports(Object handler){ return (handler instanceof HNHandlerMapping); }

    public HNModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HNHandlerMapping handlerMapping = (HNHandlerMapping) handler;

        // 把方法的形参列表和request的参数列表所在顺序进行一一对应
        Map<String,Integer> paramIndexMapping = new HashMap<String, Integer>();

        Annotation[][] pa = handlerMapping.getMethod().getParameterAnnotations();
        for (int i=0; i<pa.length; i++) {
            for (Annotation annotation : pa[i]) {
                if (annotation instanceof HNRequestMapping) {
                    String paramName = ((HNRequestMapping) annotation).value();
                    if (!"".equals(paramName.trim())) {
                        paramIndexMapping.put(paramName, i);
                        break;
                    }
                }
            }
        }

        Class<?>[] parameterTypes = handlerMapping.getMethod().getParameterTypes();
        for (int i=0; i<parameterTypes.length; i++) {
            Class<?> type = parameterTypes[i];
            if(type == HttpServletRequest.class ||
                    type == HttpServletResponse.class){
                paramIndexMapping.put(type.getName(),i);
            }
        }

        Map<String, String[]> params = request.getParameterMap();

        Object[] paramValues = new Object[parameterTypes.length];

        for (Map.Entry<String, String[]> param : params.entrySet()) {
            String value = Arrays.toString(param.getValue());
            if (!paramIndexMapping.containsKey(param.getKey())) {
                continue;
            }

            Integer index = paramIndexMapping.get(param.getKey());
            paramValues[index] = caseStringValue(value, parameterTypes[index]);
        }

        if(paramIndexMapping.containsKey(HttpServletRequest.class.getName())) {
            int reqIndex = paramIndexMapping.get(HttpServletRequest.class.getName());
            paramValues[reqIndex] = request;
        }

        if(paramIndexMapping.containsKey(HttpServletResponse.class.getName())) {
            int respIndex = paramIndexMapping.get(HttpServletResponse.class.getName());
            paramValues[respIndex] = response;
        }

        Object result = handlerMapping.getMethod().invoke(handlerMapping.getController(), paramValues);
        if(result == null || result instanceof Void){ return null; }
        boolean isModelAndView = handlerMapping.getMethod().getReturnType() == HNModelAndView.class;
        // TODO 这个地方 Spring mvc 是如何处理其他返回类型的还需要研究
        if (isModelAndView) {
            return (HNModelAndView)result;
        }
        return null;
    }

    private Object caseStringValue(String value, Class<?> parameterType) {
        if(String.class == parameterType){
            return value;
        }
        //如果是int
        if (Integer.class == parameterType) {
            return Integer.valueOf(value);
        } else if(Double.class == parameterType) {
            return Double.valueOf(value);
        } else {
            if(value != null){
                return value;
            }
            return null;
        }
    }
}
